package com.titar.kslikeview.widigets;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class NumberRainItem extends View {


    public NumberRainItem(Context context) {
        this(context, null);
    }

    public NumberRainItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumberRainItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initPaint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 获取宽-测量规则的模式和大小
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        // 获取高-测量规则的模式和大小
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        // 设置wrap_content的默认宽 / 高值
        // 默认宽/高的设定并无固定依据,根据需要灵活设置
        // 类似TextView,ImageView等针对wrap_content均在onMeasure()对设置默认宽 / 高值有特殊处理,具体读者可以自行查看
        int mWidth = tvSize;

        setMeasuredDimension(mWidth, heightSize);
    }

    private Paint paint;
    public static int tvSize = 65;


    private void initPaint() {
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setTextSize(tvSize);
    }

    private int nowHeight = 0;

    private boolean isShowAll() {
        if (nowHeight >= getHeight()) {
            return true;
        } else {
            return false;
        }
    }

    private int heightLightIndex = 1;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (nowHeight / tvSize == 0) {
            int i = (int) (Math.random() * 10);
            nowHeight = nowHeight + tvSize;
            postInvalidateDelayed(i * 100);
        } else {
            if (isShowAll()) {
                drawAll(canvas);
                heightLightIndex = (++heightLightIndex) % (getHeight() / tvSize);
            } else {
                drawLess(canvas);
                heightLightIndex++;
            }

            postInvalidateDelayed(100);
        }
    }

    private void drawAll(Canvas canvas) {
        int showCount = getHeight() / tvSize;
        for (int k = showCount; k > 0; k--) {
            if (k == heightLightIndex) {
                paint.setColor(Color.WHITE);
            } else {
                paint.setColor(Color.GREEN);
            }
            canvas.drawText((int) (Math.random() * 10) + "", 0, k * tvSize, paint);
        }
    }


    private void drawLess(Canvas canvas) {
        int showCount = nowHeight / tvSize;
        nowHeight = nowHeight + tvSize;
        for (int k = showCount; k > 0; k--) {
            if (k == heightLightIndex) {
                paint.setColor(Color.WHITE);
            } else {
                paint.setColor(Color.GREEN);
            }
            canvas.drawText((int) (Math.random() * 10) + "", 0, k * tvSize, paint);
        }
    }

}
